package com.xuexie.mr;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

public class WeatherMapper extends Mapper<LongWritable, Text, MyBean, Text>{

	public static SimpleDateFormat SDF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	protected void map(LongWritable key, Text value,
			Context context)
			throws IOException, InterruptedException {
		//把每行数据中的年，月，温度，抽取出来封装bean，然后输出。
		String line =value.toString();
		String[]  strs =StringUtils.split(line,'\t');
		try {
			Date date =SDF.parse(strs[0]);
			double hot =Double.parseDouble(strs[1].substring(0,strs[1].length()-1));
			
			java.util.Calendar c =Calendar.getInstance();
			c.setTime(date);
			
			int year=c.get(Calendar.YEAR);
			int month=c.get(Calendar.MONTH);
			
			MyBean  k =new MyBean();
			k.setHot(hot);
			k.setYear(year);
			k.setMounth(month);
			
			context.write(k, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}	
}
